package com.yx.cloud.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iot.product.sweeper.bean.CleanRecord;
import com.iot.product.sweeper.config.SweeperProductConfig;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.product.ProductConfigConstants;

import org.json.JSONObject;

import java.util.List;

public class CleanRecordAdapter extends BaseAdapter {
    private static final String TAG = "CleanRecordAdapter";
    Context mContext;
    List<CleanRecord> mList;

    public CleanRecordAdapter(Context context, List<CleanRecord> list) {
        mContext = context;
        mList = list;
    }

    public void update(List<CleanRecord> recordList) {
        mList = recordList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        Log.d(TAG, "e  vvvvvvvv getView : ");
        ViewHolder mHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.cleanrecord_item, null);

            mHolder = new ViewHolder();
//            mHolder.mIvSignIcon = convertView.findViewById(R.id.iv_signIcon);
            mHolder.mDate = convertView.findViewById(R.id.tv_date);
            mHolder.mArea = convertView.findViewById(R.id.tv_area);
            mHolder.mStartMode = convertView.findViewById(R.id.tv_clean_record_start_mode);
            mHolder.mDuration = convertView.findViewById(R.id.tv_duration);
            mHolder.mll = convertView.findViewById(R.id.ll_clean_record);
            mHolder.mRecordDetail = convertView.findViewById(R.id.iv_record_detail);
            convertView.setTag(mHolder);
            JSONObject productConfig = AppContext.getConfig();

            if (!productConfig.optBoolean(ProductConfigConstants.hasMap)) {
                mHolder.mRecordDetail.setVisibility(View.GONE);
            } else {
                mHolder.mRecordDetail.setVisibility(View.VISIBLE);
            }
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        CleanRecord bean = mList.get(position);
        Log.i(TAG, " 清扫 时间戳" + bean.getStartTime());
        if(AppContext.getConfig().optBoolean(ProductConfigConstants.isInxni)){
            //苗苗翻怪  这里时间不显示秒
            String startTime=bean.getStartTime();
            mHolder.mDate.setText(startTime.substring(0,startTime.length()-3));
        }else{
            mHolder.mDate.setText(bean.getStartTime());
        }
        mHolder.mArea.setText(bean.getArea()+"");
        mHolder.mDuration.setText(bean.getCleanTime()+"");
        mHolder.mll.setTag(position);
//        if (bean.getIsDoneNormal() == 1) {
//            mHolder.mIvSignIcon.setImageResource(R.mipmap.clean_accomplish_icon);
//        } else if (bean.getIsDoneNormal() == 0) {
//            mHolder.mIvSignIcon.setImageResource(R.mipmap.clean_interrupt_icon);
//        }
        return convertView;
    }

    class ViewHolder {
        ImageView mIvSignIcon;
        TextView mDate;
        TextView mArea;
        TextView mStartMode;
        TextView mDuration;
        LinearLayout mll;
        ImageView mRecordDetail;
    }
}
